//
//  UILabel+DH.m
//  DCProject
//
//  Created by Apple on 2020/10/13.
//  Copyright © 2020 bigbing. All rights reserved.
//

#import "UILabel+DH.h"

@implementation UILabel (DH)




//行高
static char kAssociatedObjectKey_lineHeight;
- (void)setDc_lineHeight:(CGFloat)dc_lineHeight{
    if (dc_lineHeight <= 0) {
        objc_setAssociatedObject(self, &kAssociatedObjectKey_lineHeight, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    } else {
        objc_setAssociatedObject(self, &kAssociatedObjectKey_lineHeight, @(dc_lineHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    if(self.text && dc_lineHeight > 0){
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
        
        //设置行高
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 0;  //设置行间距
        paragraphStyle.maximumLineHeight = self.dc_lineHeight;
        paragraphStyle.minimumLineHeight = self.dc_lineHeight;
        paragraphStyle.alignment = self.textAlignment;
        paragraphStyle.lineBreakMode = self.lineBreakMode;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
        
        //设置文字上下居中，不添加此代码文字会偏上/偏下
        CGFloat baselineOffset = (self.dc_lineHeight - self.font.lineHeight)/4.0;
        [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:NSMakeRange(0, attributedString.length)];
        
        self.attributedText = attributedString;
    }
}

-(CGFloat)dc_lineHeight{
    if ([self _hasSetDCLineHeight]) {
        return [(NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_lineHeight) floatValue];
    } else if (self.attributedText.length) {
        __block NSMutableAttributedString *string = [self.attributedText mutableCopy];
        __block CGFloat result = 0;
        [string enumerateAttribute:NSParagraphStyleAttributeName inRange:NSMakeRange(0, string.length) options:0 usingBlock:^(NSParagraphStyle *style, NSRange range, BOOL * _Nonnull stop) {
            // 如果用户已经通过传入 NSParagraphStyle 对文字整个 range 设置了行高，则这里不应该再次调整行高
            if (NSEqualRanges(range, NSMakeRange(0, string.length))) {
                if (style && (style.maximumLineHeight || style.minimumLineHeight)) {
                    result = style.maximumLineHeight;
                    *stop = YES;
                }
            }
        }];
        
        return result == 0 ? self.font.lineHeight : result;
    } else if (self.text.length) {
        return self.font.lineHeight;
    }
    return 0;
}
- (BOOL)_hasSetDCLineHeight {
    return !!objc_getAssociatedObject(self, &kAssociatedObjectKey_lineHeight);
}
-(void)setDc_text:(NSString *)dc_text{
    self.text = dc_text;
    if(self.text && self.dc_lineHeight > 0){
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];

        //设置行高
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 0;  //设置行间距
        paragraphStyle.maximumLineHeight = self.dc_lineHeight;
        paragraphStyle.minimumLineHeight = self.dc_lineHeight;
        paragraphStyle.alignment = self.textAlignment;
        paragraphStyle.lineBreakMode = self.lineBreakMode;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];

        //设置文字上下居中，不添加此代码文字会偏上/偏下
        CGFloat baselineOffset = (self.dc_lineHeight - self.font.lineHeight)/4.0;
        [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:NSMakeRange(0, attributedString.length)];

        self.attributedText = attributedString;
    }
}

//#pragma mark =====简化设置=====
//+(UILabel *)dc_init{
//    UILabel * view = UILabel.alloc.init;
//    return view;
//}
//-(DHLabelNSStringBlock)dc_textString{
//    return ^(NSString * dc_text){
//        self.text = dc_text;
//        if(self.text && self.dc_lineHeight > 0){
//            NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
//            
//            //设置行高
//            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//            paragraphStyle.lineSpacing = 0;  //设置行间距
//            paragraphStyle.maximumLineHeight = self.dc_lineHeight;
//            paragraphStyle.minimumLineHeight = self.dc_lineHeight;
//            paragraphStyle.alignment = self.textAlignment;
//            paragraphStyle.lineBreakMode = self.lineBreakMode;
//            [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
//            
//            //设置文字上下居中，不添加此代码文字会偏上/偏下
//            CGFloat baselineOffset = (self.dc_lineHeight - self.font.lineHeight)/4.0;
//            [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(baselineOffset) range:NSMakeRange(0, attributedString.length)];
//            
//            self.attributedText = attributedString;
//        }
//        return self;
//    };
//}
//
//-(DHLabelCGFloatBlock)dc_rowHeight{
//    return ^(CGFloat f){
//        self.dc_lineHeight = f;
//        return self;
//    };
//}
//
//- (DHLabelColorBlock)dc_textColor{
//    return ^(UIColor * color){
//        self.textColor = color;
//        return self;
//    };
//}
//-(DHLabelNSStringBlock)dc_textColorHex{
//    return ^(NSString * colorHex){
//        
//        NSString *cString = [[colorHex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
//        
//        // String should be 6 or 8 characters
//        if ([cString length] > 6) {
//            // 判断前缀
//            if ([cString hasPrefix:@"0X"])
//                cString = [cString substringFromIndex:2];
//            if ([cString hasPrefix:@"#"])
//                cString = [cString substringFromIndex:1];
//            if ([cString length] != 6)
//                return self;
//            // 从六位数值中找到RGB对应的位数并转换
//            NSRange range;
//            range.location = 0;
//            range.length = 2;
//            //R、G、B
//            NSString *rString = [cString substringWithRange:range];
//            range.location = 2;
//            NSString *gString = [cString substringWithRange:range];
//            range.location = 4;
//            NSString *bString = [cString substringWithRange:range];
//            // Scan values
//            unsigned int r, g, b;
//            [[NSScanner scannerWithString:rString] scanHexInt:&r];
//            [[NSScanner scannerWithString:gString] scanHexInt:&g];
//            [[NSScanner scannerWithString:bString] scanHexInt:&b];
//            
//            self.textColor = [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0];
//        }
//        
//        return self;
//    };
//}
//-(DHLabelFontBlock)dc_font{
//    return ^(UIFont * font){
//        self.font = font;
//        return self;
//    };
//}
//- (DHLabelCGFloatBlock)dc_fontSize{
//    return ^(CGFloat f){
//        self.font = [UIFont systemFontOfSize:f weight:UIFontWeightRegular];
//        return self;
//    };
//}
//- (DHLabelCGFloatBlock)dc_fontSizeMedium{
//    return ^(CGFloat f){
//        self.font = [UIFont systemFontOfSize:f weight:UIFontWeightMedium];
//        return self;
//    };
//}
//- (DHLabelCGFloatBlock)dc_fontSizeSemibold{
//    return ^(CGFloat f){
//        self.font = [UIFont systemFontOfSize:f weight:UIFontWeightSemibold];
//        return self;
//    };
//}
//- (DHLabelCGFloatBlock)dc_fontSizeBold{
//    return ^(CGFloat f){
//        self.font = [UIFont systemFontOfSize:f weight:UIFontWeightBold];
//        return self;
//    };
//}
//
//-(DHLabelNSTextAlignmentBlock)dc_textAlignment{
//    return ^(NSTextAlignment textAlignment){
//        self.textAlignment = textAlignment;
//        return self;
//    };
//}
//-(DHLabelNSIntegerBlock)dc_numberOfLines{
//    return ^(NSInteger intValue){
//        self.numberOfLines = intValue;
//        return self;
//    };
//}
//-(DHLabelBOOLBlock)dc_userInteractionEnabled{
//    return ^(BOOL boolValue){
//        self.userInteractionEnabled = boolValue;
//        return self;
//    };
//}

@end
